﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 玩具商城管理系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Class1 shopcarSrv = new Class1();
        public static int flag = 1;
        public static bool Aflag = false;//false为未登录，true为已登录
        public static string Commodityid = "";
        SqlDataAdapter  daProduct;
       
        void showAllProduct()
        {
            DB.GetCn();
            string str = "select*from commodity";
            SqlCommand cmd=new SqlCommand(str,DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                int index = SP.Rows.Add();
                SP.Rows[index].Cells[0].Value = rdr[0];
                SP.Rows[index].Cells[1].Value = rdr[1];
                SP.Rows[index].Cells[2].Value = rdr[2];
                SP.Rows[index].Cells[3].Value = rdr[3];
                SP.Rows[index].Cells[4].Value = rdr[4];
                SP.Rows[index].Cells[5].Value = rdr[5];
                SP.Rows[index].Cells[6].Value = rdr[6];
                try
                {
                    Image imageColumn = Image.FromFile(Application.StartupPath + rdr[8]);
                    SP.Rows[index].Cells["Column1"].Value = imageColumn;
                }
                catch
                {
                    Image imgeColumn = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                    SP.Rows[index].Cells["Column1"].Value = imgeColumn;
                }
            }
            rdr.Close();
        }
        private void WindowsSP_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int CIndex = e.ColumnIndex;
            if (CIndex == 8)
            {
                int rowindex = e.RowIndex;
                Commodityid = SP.Rows[rowindex].Cells[0].Value.ToString();
                Tcommodity tl=new Tcommodity();
                tl.ShowDialog();    
            }
            if (CIndex == 9)
            {
                if (newwin.Aflag == false)
                {
                    MessageBox.Show("请先登录");
                    login ll = new login(); 
                    ll.ShowDialog();
                }
                else
                {
                    int rowindex = e.RowIndex;
                    Commodityid= SP.Rows[rowindex].Cells[0].Value.ToString();
                    newwin.Commodityid = Commodityid;
                    if (Convert.ToInt32(SP.Rows[rowindex].Cells[3].Value)< 1)
                    {
                        MessageBox.Show("该商品暂时没有库存");
                    }
                    else
                    {
                        SP.Rows[rowindex].Cells[3].Value = Convert.ToInt32(SP.Rows[rowindex].Cells[3].Value) - 1;
                        shopcarSrv.Add(login.username, Convert.ToInt32(newwin.Commodityid), 1);
                        Tshopcar sl = new Tshopcar();
                        sl.ShowDialog();
                       
                    }
                   
                }
               
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {
           
            SP.RowTemplate.Height = 90;
            showAllProduct();
        }

        private void xianshiquanbu_Click(object sender, EventArgs e)
        {
            SP.Rows.Clear();
            showAllProduct();
           
        }

        private void chaxun_Click(object sender, EventArgs e)//按商品名称查询
        {
            if(txtsp.Text=="")
            {
                MessageBox.Show("请输入查询关键字");
            }
            else
            {
                SP.Rows.Clear();
                DB.GetCn();
                string str ="select * from commodity where commodity_name like '%"+txtsp.Text +"%'";
                SqlCommand cmd =new SqlCommand(str, DB.cn);
                SqlDataReader rdr = cmd.ExecuteReader();
                while(rdr.Read())
                {
                    int index = SP.Rows.Add();
                    SP.Rows[index].Cells[0].Value = rdr[0];
                    SP.Rows[index].Cells[1].Value = rdr[1];
                    SP.Rows[index].Cells[2].Value = rdr[2];
                    SP.Rows[index].Cells[3].Value = rdr[3];
                    SP.Rows[index].Cells[4].Value = rdr[4];
                    SP.Rows[index].Cells[5].Value = rdr[5];
                    SP.Rows[index].Cells[6].Value = rdr[6];
                    try
                    {
                        Image imageColumn = Image.FromFile(Application.StartupPath + rdr[8]);
                        SP.Rows[index].Cells["Column1"].Value = imageColumn;
                    }
                    catch
                    {
                        Image imgeColumn = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                        SP.Rows[index].Cells["Column1"].Value=imgeColumn;
                    }
                }
                rdr.Close();
            }
        }

        private void chaxun2_Click(object sender, EventArgs e)//按商品价格区间查询
        {
            SP.Rows.Clear();
            string str = "";
            DB.GetCn();
            int i = comboBox1.SelectedIndex;
            switch(i) 
            {
                case 0:
                    str = " commodity_money<=70 ";
                    break;
                case 1:
                    str = " commodity_money>70 and commodity_money<=100 ";
                    break;
                case 2:
                    str = " commodity_money>100 and commodity_money<=150 ";
                    break;
                case 3:
                    str = " commodity_money>150 and commodity_money<=200 ";
                    break;
                case 4:
                    str = " commodity_money>200 ";
                    break;
                case 5:
                    str = " sort_name='"+comboBox1.Text+"'";
                    break;
                case 6:
                    str = " sort_name='"+comboBox1.Text+"'";
                    break;
                case 7:
                    str = " sort_name='"+comboBox1.Text+"'";
                    break;


            }
            string sdr="select * from commodity where " + str + "";
            SqlCommand cmd = new SqlCommand(sdr,DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                int index = SP.Rows.Add();
                SP.Rows[index].Cells[0].Value = rdr[0];
                SP.Rows[index].Cells[1].Value = rdr[1];
                SP.Rows[index].Cells[2].Value = rdr[2];
                SP.Rows[index].Cells[3].Value = rdr[3];
                SP.Rows[index].Cells[4].Value = rdr[4];
                SP.Rows[index].Cells[5].Value = rdr[5];
                SP.Rows[index].Cells[6].Value = rdr[6];
                try
                {
                    Image imageColumn = Image.FromFile(Application.StartupPath + rdr[8]);
                    SP.Rows[index].Cells["Column1"].Value = imageColumn;
                }
                catch
                {
                    Image imgeColumn = Image.FromFile(Application.StartupPath + "//" + "暂无图片.gif.jpg");
                    SP.Rows[index].Cells["Column1"].Value = imgeColumn;
                }
            }
            rdr.Close();
        }

        private void 登录按钮_Click(object sender, EventArgs e)
        {
            this.Hide();
            login ll = new login();
            ll.ShowDialog();
            if (flag == 1)
            {
                newwin.flag=1;
                    label5.Text = login.username;
                this.Visible = true;
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void 注册按钮_Click(object sender, EventArgs e)
        {
            this.Hide();
            register rl =new register();    
            rl.ShowDialog();
            if (flag == 1)
            {
                
                this.Visible = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            newwin qq =new newwin();
            qq.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            znew.Ncommodity ncommodity = new znew.Ncommodity();
            ncommodity.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
